<?php

function db_exec($str)
{
 $database = "obscure";
 $host = "localhost";
 $user = "paulo";
 $password = "obscure";

 $con = mysql_connect($host,$user,$password);
 if (!$con)
 {
  die('Could not connect: ' . mysql_error());
 }
 mysql_select_db($database, $con);
 $res = mysql_query($str,$con);
 if(!$res)
  echo 'error on DB: '.$res;
 mysql_close($con);
 return $res;
}

function db_query($query)
{
 $result = db_exec($query);
 $list = array();
 if($result)
 {
  while($row = mysql_fetch_array($result))
  {
   array_push($list,$row);
  }
 }
 return $list;
}

//========IMPLEMENTATION=========

$query_find_person_type = "";
$query_page_size = 12;

function db_get_query_find_person_type()
{
 global $query_find_person_type;
 return $query_find_person_type;
}

function db_find_person_type_add($data)
{
 global $query_find_person_type;

 if(empty($data))
  return false;
 if(!empty($query_find_person_type))
  $query_find_person_type .= '%';
 $query_find_person_type .= htmlentities($data,ENT_QUOTES);
 return true;
}

function db_find_person_type_searchstring($searchstring)
{
 global $query_find_person_type;
 $query_find_person_type = htmlentities($searchstring,ENT_QUOTES);
}

function db_find_person_query_exists()
{
 global $query_find_person_type;
 if(empty($query_find_person_type))
  return false;
 return true; 
}

function db_find_person_type($page)
{
 global $query_find_person_type;
 global $query_page_size;
 $query = 'SELECT * FROM person WHERE type LIKE "%'.$query_find_person_type.'%" ORDER BY name ASC LIMIT '.($page*$query_page_size).', '."$query_page_size";
 return db_query($query);
}

function db_find_person_type_all($page)
{
 global $query_page_size;
 $query = 'SELECT * FROM person ORDER BY name ASC LIMIT '.($page*$query_page_size).', '."$query_page_size";
 return db_query($query);
}


function db_find_person_by_id($id)
{
 $query = 'SELECT * FROM person WHERE id = '.htmlentities($id,ENT_QUOTES);
 return db_query($query);
}
